package top.csy8888.ch10.service;

import org.apache.logging.log4j.message.SimpleMessage;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;
import top.csy8888.ch10.dto.Message;

import java.util.Date;

/**
 * @author CSY
 * @date 2025 - 09 - 19
 * 消息的发布服务
 */
@Service
public class PublishService {

    /**
     * 注入SimpMessagingTemplate，用于发布消息
     */
    private SimpMessagingTemplate template;

    public PublishService(SimpMessagingTemplate template) {
        this.template = template;
    }

    /**
     * 消息投递
     * @param message
     */
    public void sendMessage(String message){
        Message msg = new Message();
        msg.setContent(message);
        msg.setSendDate(new Date());
        //将消息发布到Broker中,/news是配置的主题前缀，/message是自定义的路径
        template.convertAndSend("/news/message", msg);
    }
}
